package com.hotel.project.system.customer.service;

import com.hotel.project.system.guestroom.domain.GuestRoom;
import com.hotel.project.system.guestroom.service.IGuestRoomService;
import com.hotel.project.system.roomorders.domain.RoomOrders;
import com.hotel.project.system.roomorders.service.IRoomOrdersService;
import com.hotel.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zy
 * @date 2019-04-06 21:31
 **/
@Service
public class CustomerService {

    @Autowired
    private IGuestRoomService guestRoomService;
    @Autowired
    private IRoomOrdersService roomOrdersService;

    @Transactional(rollbackFor = Exception.class)
    public int reserve(int roomId, int stopDay, User user) {
        GuestRoom guestRoom = guestRoomService.selectGuestRoomById(roomId);
        guestRoom.setStatus(1);
        guestRoomService.updateGuestRoom(guestRoom);
        RoomOrders roomOrders = new RoomOrders();
        roomOrders.setRoomId(roomId);
        roomOrders.setRoomCode(guestRoom.getCode());
        roomOrders.setAmount(guestRoom.getPrice());
        roomOrders.setUserId(user.getUserId().intValue());
        roomOrders.setUserName(user.getUserName());
        roomOrders.setStopDay(stopDay);
        roomOrders.setStatus(1);
        return roomOrdersService.insertRoomOrders(roomOrders);
    }

    @Transactional(rollbackFor = Exception.class)
    public int cancel(int orderId) {
        RoomOrders roomOrders = roomOrdersService.selectRoomOrdersById(orderId);
        roomOrders.setStatus(0);
        roomOrders.setStopDay(0);
        roomOrdersService.updateRoomOrders(roomOrders);
        GuestRoom guestRoom = guestRoomService.selectGuestRoomById(roomOrders.getRoomId());
        guestRoom.setStatus(0);
        return guestRoomService.updateGuestRoom(guestRoom);
    }
}
